package com.example.limiting.taskmanager2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * Created by Limiting on 2015/9/24.
 */
public class TaskAdapter extends ArrayAdapter<Task> {
    private Context mContext;
    private int mResourceId;
    public TaskAdapter(Context context,int textViewResourceId){
        super(context, textViewResourceId);
        this.mContext = context;
        this.mResourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if(null == convertView){
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);

            convertView = inflater.inflate(mResourceId, null);
            viewHolder.titleText = (TextView) convertView.findViewById(R.id.task_title);
            viewHolder.startTimeText = (TextView) convertView.findViewById(R.id.task_start_time);
            viewHolder.endTimeText = (TextView) convertView.findViewById(R.id.task_end_time);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Task task = getItem(position);

        if(null != task){
            viewHolder.titleText.setText(task.getTitle());
            viewHolder.titleText.setTag(task.getTaskId());
            viewHolder.startTimeText.setText(task.getStartTime());
            viewHolder.endTimeText.setText(task.getEndTime());
        }


        return convertView;
    }

    static class ViewHolder{
        TextView titleText;
        TextView startTimeText;
        TextView endTimeText;
    }
}
